using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using Lidgren.Network;

public class BoardRemote : Board
{

	#region public

	[Serializable]
	public class ActionQueue
	{
		public enum Type
		{
			Main,
			New}

		;

		public Type type;
		public Queue<Board.Action> queue;

		public ActionQueue(Queue<Board.Action> queue, Type type)
		{
			this.queue = queue;
			this.type = type;
		}
	}
	//Board display initialization
	public override void Init(GameManager gm, int width, int height)
	{
		if (gm == null)
		{
			Debug.LogException(new System.Exception("Transferred GameManager reference is null!"));
		}
		m_GameManager = gm;
		m_BoardDisplay = gm.mainBoardDisplay;
		if (m_BoardDisplay == null)
		{
			Debug.LogException(new System.Exception("Main Board Display is null!"));
		}
		m_Client = gm.mainClient;
		if (m_Client == null || m_Client.ConnectionStatus != Lidgren.Network.NetConnectionStatus.Connected)
		{
			Debug.LogException(new System.Exception("Main network client is null or not connected!"));
		}
		m_Client.OverrideDataHandler(typeof(ActionQueue), (message, data, instance) =>
		{
			ActionQueue queue = data as ActionQueue;
			switch (queue.type)
			{
				case ActionQueue.Type.Main:
					InvokeOnMainActionChain(queue.queue);
					break;
				case ActionQueue.Type.New:
					InvokeOnNewActionChain(queue.queue);
					break;
				default:
					Debug.LogWarning("Unknown action queue type!");
					break;
			}
		});
	}
	// Main function that handles player input and make board correspond in manner appropriate to game mode
	public override void MakeSwap(int x1, int y1, int x2, int y2, int playerID = 0)
	{
		m_Client.SendObject(new Move(x1, y1, x2, y2, playerID), NetDeliveryMethod.ReliableOrdered);
	}

	#endregion

	#region protected

	protected AutoClient m_Client;

	protected void Update()
	{
		m_Client.DispatchMessages();
	}

	#endregion

}